﻿/*!	@file	r_gui_widget_spin.cpp
	@brief	Window_GUI
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_gui_widget_spin.h"
#include "r_gui_server.h"
#include "r_gui_draw.h"
#include "r_gui_event.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace gui{
RRTTI_REFLECTION_IMPLEMENT ( r::gui::WidgetSpin, r::gui::Widget )
// <<<<<<<<<< RTTI <<<<<<<<<<

		//! 感知閾値
		static	const	r_si32	sRGUIWidgetSpinThreashould	= 4;
		//! Y軸倍率
		static	const	r_si32	sRGUIWidgetSpinScaleY		= 5;

		WidgetSpin :: WidgetSpin()
			:
			state ( eState_None )
		{
			minWidth	= 12;
			minHeight	= 12;

			posX		= 0;
			posY		= 0;
			posW		= minWidth;
			posH		= minHeight;
			posOffR		= posOffL	= 0;
			posOffT		= posOffB	= 0;
		}

		WidgetSpin :: ~WidgetSpin()
		{
		}

		typeConnection	WidgetSpin :: connectSlotSpin ( const typeSignals<void ( r_si32 ) >::slot_type &slot )
		{
			return signalSpin.connect ( slot );
		}

		void	WidgetSpin :: onDraw ( draw::Draw& guidraw )
		{
			// 自分の領域にあわせて描画領域を作成
			guidraw.setZ ( guidraw.getZ() + 1 );
			r_si32	px;
			r_si32	py;
			r_si32	pw;
			r_si32	ph;
			r_si32	nx;
			r_si32	ny;
			r_si32	nw;
			r_si32	nh;
			guidraw.getRect ( px, py, pw, ph );
			if ( guidraw.calcRect ( px, py, pw, ph, wposX, wposY, posW, posH, nx, ny, nw, nh ) == true ) {
				guidraw.asignGUITexture();
				guidraw.setRect ( nx, ny, nw, nh );
				guidraw.asignRect();

				r_si32	windowIndex			= ( state == eState_Drag ) ? draw::WIndows_DownRArc : draw::WIndows_UpRArc;
				Server::instance		gui;
				const DrawInfo&		guiinfo = gui->getDrawInfo();
				if ( getVisible() == eRGUIVisible_Ghost ) {
					guidraw.drawWindow ( wposX, wposY, posW, posH, windowIndex, guiinfo.passivePartColor );
				} else {
					if ( getIsHover() == true ) {
						guidraw.drawWindow ( wposX, wposY, posW, posH, windowIndex, guiinfo.hoverPartColor );
					} else {
						switch ( state ) {
						case eState_None:
							guidraw.drawWindow ( wposX, wposY, posW, posH, windowIndex, guiinfo.activePartColor );
							break;
						case eState_Drag:
							guidraw.drawWindow ( wposX, wposY, posW, posH, windowIndex, guiinfo.dragPartColor );
							break;
						}
					}
				}
				// icon描画
				guidraw.drawIcon ( wposX, wposY, draw::Icons_Spin, guiinfo.activePartColor );
			}
			// 親の領域を復帰させる
			guidraw.setZ ( guidraw.getZ() - 1 );
			guidraw.setRect ( px, py, pw, ph );
		}

		void	WidgetSpin :: onClickInRectLMB ( Event& guiEvent )
		{
			guiEvent.setLMBDrag ( this );
			state				= eState_Drag;
			lastSpin			= 0;
		}

		void	WidgetSpin :: onReleaseInRectLMB ( Event& guiEvent )
		{
			state				= eState_None;
		}

		void	WidgetSpin :: onReleaseOutRectLMB ( Event& guiEvent )
		{
			state				= eState_None;
		}

		void	WidgetSpin :: onDragLMB ( Event& guiEvent )
		{
			state				= eState_Drag;
			// シグナル
			r_si32	dragX;
			r_si32	dragY;
			guiEvent.getDragPos ( dragX, dragY );
			r_si32	curX;
			r_si32	curY;
			guiEvent.getCurPos ( curX, curY );
			r_si32	xdiff		= curX - dragX;
			r_si32	ydiff		= dragY - curY;
			r_si32	diff		= ( xdiff / sRGUIWidgetSpinThreashould ) + ( ( ydiff / sRGUIWidgetSpinThreashould ) * sRGUIWidgetSpinScaleY );
			signalSpin ( diff - lastSpin );
			lastSpin			= diff;
		}
	}
}


